package com.ruicar.afs.cloud.apply.pre.loan.mq.sender.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.PreApproveInfo;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ComAttachmentFileDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.approveprev.ApprovePrevInfoDto;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 预审批MQ报文组装
 *
 * @author 耿继辉
 */
@Service
@Slf4j
@AllArgsConstructor
public class PreApproveSenderImpl {
    private final ComAttachmentFileService comAttachmentFileService;

    /**
     * 预审批提交报文组装
     *
     * @param preApproveInfo 预审批信息
     * @return
     */
    public AfsTransEntity<ApprovePrevInfoDto> returnPreApproveInfo(PreApproveInfo preApproveInfo) {
        AfsTransEntity<ApprovePrevInfoDto> transEntity = new AfsTransEntity<>();
        ApprovePrevInfoDto approvePrevInfoDto = new ApprovePrevInfoDto();
        //查询预审批附件信息
        List<ComAttachmentFile> list = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                .eq(ComAttachmentFile::getBusiNo, String.valueOf(preApproveInfo.getId()))
                .eq(ComAttachmentFile::getBelongNo, String.valueOf(preApproveInfo.getId())));
        List<ComAttachmentFileDto> comAttachmentFileDtoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(comAttachmentFile -> {
                ComAttachmentFileDto comAttachmentFileDto = new ComAttachmentFileDto();
                BeanUtil.copyProperties(comAttachmentFile, comAttachmentFileDto);
                comAttachmentFileDtoList.add(comAttachmentFileDto);
            });
        }
        Map<String, String> map = this.returnBirthdayAndSex(preApproveInfo.getCertNo());
        approvePrevInfoDto.setApplyId(String.valueOf(preApproveInfo.getId()));
        approvePrevInfoDto.setApplyDate(preApproveInfo.getApplyDate());
        approvePrevInfoDto.setBusinessType(preApproveInfo.getBusinessType());
        approvePrevInfoDto.setCertEndDate(preApproveInfo.getCertEndDate());
        approvePrevInfoDto.setCertNo(preApproveInfo.getCertNo());
        approvePrevInfoDto.setCertType("00001");
        approvePrevInfoDto.setChannelCode(String.valueOf(preApproveInfo.getChannelId()));
        approvePrevInfoDto.setChannelName(preApproveInfo.getChannelName());
        approvePrevInfoDto.setPhone(preApproveInfo.getTelPhone());
        approvePrevInfoDto.setSex(map.get("sex"));
        approvePrevInfoDto.setComAttachmentFileDtoList(comAttachmentFileDtoList);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String birthday = map.get("birthday");
            Date date = simpleDateFormat.parse(birthday);
            approvePrevInfoDto.setBirthday(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        approvePrevInfoDto.setSellerPhone(preApproveInfo.getSellerPhone());
        approvePrevInfoDto.setSellerRealName(preApproveInfo.getSellerRealName());
        approvePrevInfoDto.setSellerName(preApproveInfo.getCreateBy());
        approvePrevInfoDto.setCustomerName(preApproveInfo.getCustName());
        approvePrevInfoDto.setApplyNo(preApproveInfo.getApplyNo());
        transEntity.setData(approvePrevInfoDto);
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_PREAPPROVE_INFO_PUSH);
        return transEntity;
    }

    /**
     * 返回出生日期，性别
     *
     * @return
     */
    public Map<String, String> returnBirthdayAndSex(String certNo) {
        Map<String, String> map = new HashMap<>();
        String birthday = "";
        String age = "";
        String sex = "";
        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certNo.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag) {
                    return new HashMap<>();
                }
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag) {
                    return new HashMap<>();
                }
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certNo.length() == 15) {
            birthday = "19" + certNo.substring(6, 8) + "-"
                    + certNo.substring(8, 10) + "-"
                    + certNo.substring(10, 12);
            sex = Integer.parseInt(certNo.substring(certNo.length() - 3, certNo.length())) % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt("19" + certNo.substring(6, 8))) + "";
        } else if (flag && certNo.length() == 18) {
            birthday = certNo.substring(6, 10) + "-"
                    + certNo.substring(10, 12) + "-"
                    + certNo.substring(12, 14);
            sex = Integer.parseInt(certNo.substring(certNo.length() - 4, certNo.length() - 1)) % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt(certNo.substring(6, 10))) + "";
        }
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sex", sex);
        log.info("出生日期-年龄-性别:{}", map);
        return map;
    }
}
